package msu.ml.data.level2;

import java.util.*;

public class Scan
{
   private ArrayList<Ray> rays;
   private IVolumeCoveragePattern vcp;
   private int scanIndex;

   public Scan(int index)
   {
      this.rays = new ArrayList<Ray>();
      this.scanIndex = index;
   }

   public Scan()
   {
      this(0);
   }

   public void addRay(Ray value)
   {
      this.rays.add(value);
   }

   public Ray getRay(int i)
   {
      if(i < 0)
         i = this.rays.size() + i;
      else if(i >= this.rays.size())
         i = i % this.rays.size();

      return this.rays.get(i);
   }

   public int numRays()
   {
      return this.rays.size();
   }

   public Date getTime()
   {
      if(numRays() > 0)
         return getRay(0).getTime();
      return new Date();
   }

   public boolean isReflectivityScan()
   {
      return this.vcp.isReflectivityScan(this.scanIndex);
   }

   public boolean isDopplerScan()
   {
      return this.vcp.isDopplerScan(this.scanIndex);
   }

   public void setVolumeCoveragePattern(IVolumeCoveragePattern vcp)
   {
      this.vcp = vcp;
   }

   public IVolumeCoveragePattern getVolumeCoveragePattern()
   {
      return this.vcp;
   }

   public void sortRays()
   {
      Collections.sort(rays, new RaySorter());
   }

   private class RaySorter implements Comparator<Ray>
   {
      public int compare(Ray a, Ray b)
      {
         double az1 = a.getAzimuth();
         double az2 = b.getAzimuth();

         if(az1 == az2)
            return 0;
         else if(az1 < az2)
            return -1;
         else
            return 1;
      }
   }
}
